package com.intermancer.predictor.inject;

import com.intermancer.predictor.store.StoreEntry;
import com.intermancer.predictor.store.StoreEntryDAO;

public class DefaultInjectStrategy implements InjectStrategy {

	@Override
	public StoreEntry inject(StoreEntry storeEntry, StoreEntryDAO storeEntryDAO) {
		StoreEntry storedEntry = null;
		if (storeEntry.getScore() > 0.0) {
			if (storeEntryDAO.getSize() >= storeEntryDAO.getMaxSize()) {
				int targetIndex = storeEntryDAO.getScoreIndex(storeEntry
						.getScore());
				if (targetIndex == storeEntryDAO.getSize()) {
					targetIndex--;
				}
				StoreEntry condemnedEntry = storeEntryDAO
						.getByScoreIndex(targetIndex);
				storeEntryDAO.delete(condemnedEntry.getIdentity());
			}
			storedEntry = storeEntryDAO.put(storeEntry);
		}
		return storedEntry;
	}

}
